package main

import (
	"fmt"
	"io/ioutil"
	"sort"
	"strings"
	"time"
)

func main() {
	Run()
	time.Sleep(time.Minute * 30)
}

// 执行
func Run() {
	data := getFileContent("./match.txt")
	list := make(map[string]map[string]interface{}, 0)
	sdata := strings.Split(data, "\r\n")
	for _, v := range sdata {
		if strings.Contains(v, "心动佳人:") {
			arr1 := strings.Split(strings.Trim(v, " "), "心动佳人:")
			if len(arr1) == 2 {
				tag := strings.Trim(arr1[0], " ")
				arr2 := strings.Split(tag, "]")
				num := strings.Trim(arr2[0], "[")
				match := strings.Split(strings.Trim(arr1[1], " "), "-")
				list[num] = make(map[string]interface{})
				list[num]["num"] = num
				list[num]["name"] = arr2[1]
				list[num]["match"] = match
			}
		}
	}
	fmt.Println("\n\n开始筛选双向奔赴...\n\n")
	getMatch(list)
	fmt.Println("\n\n开始筛选潜伏对象...\n\n")
	getMatchLonely(list)

	fmt.Println("\n 筛选结束,感谢星星及参与同学...\n\n")
}

func getSortKey1(list map[string]map[string]interface{}) []string {
	var testSlice []string
	for key, _ := range list {
		testSlice = append(testSlice, key)
	}
	sort.Strings(testSlice)

	return testSlice
}

// 获取文件列表
func getFileContent(filename string) string {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return ""
	}

	return string(data)
}

// 获取双向匹配人员
func getMatch(list map[string]map[string]interface{}) {
	keys := getSortKey1(list)
	for _, id := range keys {
		fmt.Println(id, list[id]["name"], "------------------------------------------------------")
		for kk, _ := range list {
			if id != kk {
				data := list[kk]["match"].([]string)
				dataCurr := list[id]["match"].([]string)

				// 判读对方有没有自己
				own := false
				for _, val := range data {
					if id == val {
						own = true
					}
				}

				obj := false
				// 判读自己有没有对方
				for _, val := range dataCurr {
					if kk == val {
						obj = true
					}
				}

				if own && obj {
					// 双向奔赴 共同选择
					fmt.Println("双向奔赴", id, list[id]["name"], "======", kk, list[kk]["name"])
				}
			}
		}
	}
}

// 获取单向匹配人员
func getMatchLonely(list map[string]map[string]interface{}) {
	keys := getSortKey1(list)
	for _, id := range keys {
		fmt.Println(id, list[id]["name"], "------------------------------------------------------")
		for kk, _ := range list {
			if id != kk {
				data := list[kk]["match"].([]string)
				dataCurr := list[id]["match"].([]string)

				// 判读对方有没有自己
				own := false
				for _, val := range data {
					if id == val {
						own = true
					}
				}

				obj := false
				// 判读自己有没有对方
				for _, val := range dataCurr {
					if kk == val {
						obj = true
					}
				}

				if own && !obj {
					// 单向向奔赴 对方选择自己
					fmt.Println("潜伏对象", id, list[id]["name"], "======", kk, list[kk]["name"])
				}
			}
		}
	}
}
